package com.java.comprehensive.arithmetic.leecode.tags.array;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @Author: 窦绍飞
 * @Date: 2022/6/25
 */
public class LeetCode_26 {

	public static int removeDuplicates(int[] nums) {
		Set<Integer> set = new LinkedHashSet<>();
		for (int num : nums) {
			set.add((Integer) num);
		}
		int i = 0;
		for (Integer integer : set) {
			nums[i] = integer;
			i++;
		}
		return set.size();
	}

	public static int removeDuplicatesTwo(int[] nums) {
		int l = 0;
		int r = l + 1;
		int n = nums.length - 1;
		while (r <= n) {
			int leftNum = nums[l];
			int rightNum = nums[r];
			if (leftNum != rightNum) {
				nums[l + 1] = nums[r];
				l++;
			}
			r++;
		}
		return l + 1;
	}

	public static void main(String[] args) {
		int[] nums = {1, 1, 2};
		System.out.println(removeDuplicatesTwo(nums));
	}
}
